package com.wl2o2o.writtenview.eqianbao;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.*;

public class UnsafeLazySingleton {
    private static UnsafeLazySingleton instance;

    private UnsafeLazySingleton() {}

    public static UnsafeLazySingleton getInstance() {
        if (instance == null) {
            instance = new UnsafeLazySingleton();
        }
        return instance;
    }

    @Override
    public int hashCode() {
        return super.hashCode(); // 使用Object类默认的hashCode实现，仅用于演示目的
    }

    public void say() {
        System.out.println("i'm lazyPeople!就是玩!");
    }

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        final int LOOP_COUNT = 1000; // 设置循环次数
        ExecutorService executor = Executors.newFixedThreadPool(10); // 创建一个固定大小的线程池

        List<Future<Integer>> futures = new ArrayList<>();
        Random random = new Random();
        // 创建10个线程，每个线程执行1000次

        for (int i = 0; i < LOOP_COUNT; i++) {
            Future<Integer> future = executor.submit(() -> {
                UnsafeLazySingleton instance = UnsafeLazySingleton.getInstance();
                instance.say();
                return instance.hashCode();
            });
            futures.add(future);
            // 在这里添加一些随机延时以增加并发冲突的可能性
            Thread.sleep(random.nextInt(10)); // 随机等待时间（单位：毫秒）
        }

        for (Future<Integer> future : futures) {
            System.out.println("Hash Code: " + future.get());
        }

        executor.shutdown();
    }
}